layui.use(['jquery', 'form', 'layer'], function () {
    var $ = layui.$; // 使用jQuery
    var form = layui.form; // 获取form模块
    var layer = layui.layer; // 获取layer模块

    if (document.cookie != "") {
        // 拆分Cookie
        var arr = document.cookie.split(";"); 
        var cookieObj = {}; // cookieObj对象
        var tmp; // 临时数组 
        for (var i = 0; i < arr.length; i++) {
            tmp = arr[i].trim().split("=");
            cookieObj[tmp[0]] = tmp[1];
        }
        
        // AJAX传输
        $.ajax({
            type: "post",
            async: true,
            url: "/admin/Llogin.do",
            dataType: "json",
            data: {
                "lname": cookieObj.lname,
                "lpass": cookieObj.lpass
            },
            error: function () {
                layer.msg("ajax请求失败", {
                    icon: 5
                });
            },
            success: function (response) {
                if (response == 1) {
                    window.location.href = "html/set/index/carouse_one.html";
                } 
            }
        });
    }

    // input元素样式切换
    $('.input_border input').focus(function () {
        $(this).parent().parent().css('border-color', '#c2c2c2');
    });

    $('.input_border input').focusout(function () {
        $(this).parent().parent().css('border-color', '#e2e2e2');
    });

    // 提交表单数据
    form.on('submit(login)', function (data) {
        // 输入验证
        if (data.field.lpass == "") {
            layer.msg('密码不能为空', {
                icon: 5
            });
            return false;
        }
        if (data.field.lname == "") {
            layer.msg('账号不能为空', {
                icon: 5
            });
            return false;
        }

        // AJAX传输
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/Llogin.do",
            data: {
                "lname": data.field.lname,
                "lpass": data.field.lpass,
                "rememberme": data.field.rememberme ? data.field.rememberme : ""
            },
            dataType: "json",
            error: function () {
                layer.msg("ajax请求失败", {
                    icon: 5
                });
            },
            success: function (response) {
                if (response == 1) {
                    window.location.href = "html/set/index/carouse_one.html";
                } else {
                    layer.msg("账号或密码不正确请重新输入", {
                        icon: 5
                    });
                }
            }
        });
        
        // 阻止form默认行为
        return false;
    });
});